<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;
use app\index\model\User as thisModel;


/**
 * 
 */
class User extends Base
{
	public function index()
	{
		$where['is_del'] = 0;
		$list = DB::name('user')->where($where)->order('id desc')->paginate(15);
		
		$page = $list->render();
		$this->assign('page',$page);
		$list = $list->all();
		foreach ($list as $k => $v) {
			$list[$k]['sex']  = $v['sex'] == 1 ? '男' : '女' ;
			$list[$k]['group_name'] = DB::name('authgroup')->where('id = '.$v['group_id'])->value('title');
		}
		return $this->fetch('index',[
			'list'		=>	$list
		]);
	}


	public  function  create()
	{
		if(Request::instance()->isPost()){
			$password = Request::instance()->post('password');
			$repassword = Request::instance()->post('repassword');
			if($password != $repassword){
				$this->error('两次密码输入不一致');
				exit();
			}
			$data = Request::instance()->post();
			unset($data['repassword']);
			$data['password'] = msc($data['password']);
			$data['create_time'] = $data['update_time'] =time();
			$res = DB::name('user')->insert($data);
			if($res){
				$this->success('用户添加成功',url('index/index/index',array('con'=>'user','act'=>'index')));
			}
		}
		$prov = $this->getAddress();
		$group = $this->getAuthGroup();
		$shop = $this->getShop();
		$folder = DB::name('person_folder')->where('is_del = 0 and status = 1')->field('id,bumen,name')->order('bumen')->select();
		return $this->fetch('create',[
			'prov'		=>	$prov,
			'group'		=>	$group,
			'shop'		=>	$shop,
			'folder'	=>	$folder,
		]);
	}

	public function update($id)
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			if($data['password'] != $data['repassword']){
				$this->error('密码输入不一致');
				exit();
			}
			unset($data['repassword']);
			$id = $data['id'];
			unset($data['id']);
			if(empty($data['password'])){
				unset($data['password']);
			}else{
				$data['password'] = msc($data['password']);
			}
			$res = DB::name('user')->where('id = '.$id)->setField($data);
			if($res){
				$this->success('修改信息成功',url('index/index/index',array('con'=>'user','act'=>'index')));
			}
		}
		$data = DB::name('user')->find($id);
		$prov = $this->getAddress();
		$group = $this->getAuthGroup();
		$shop = $this->getShop();
		$city = $this->getcity($data['prov']);
		$area = $this->getarea($data['city']);
		$folder = DB::name('person_folder')->where('is_del = 0 and status = 1')->field('id,bumen,name')->order('bumen')->select();
		return $this->fetch('update',[
			'data'		=>	$data,
			'prov'		=>	$prov,
			'group'		=>	$group,
			'shop'		=>	$shop,
			'city'		=>	$city,
			'area'		=>	$area,
			'folder'	=>	$folder,
		]);
	}


	public function updateIndex($id)
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			if($data['password'] != $data['repassword']){
				$this->error('密码输入不一致');
				exit();
			}
			unset($data['repassword']);
			$id = $data['id'];
			unset($data['id']);
			if(empty($data['password'])){
				unset($data['password']);
			}else{
				$data['password'] = msc($data['password']);
			}
			$res = DB::name('user')->where('id = '.$id)->setField($data);
			if($res){
				$this->success('修改信息成功',url('index/index/index',array('con'=>'index','act'=>'main')));
			}
		}
		$data = DB::name('user')->find($id);
		$prov = $this->getAddress();
		$group = $this->getAuthGroup();
		$shop = $this->getShop();
		$city = $this->getcity($data['prov']);
		$area = $this->getarea($data['city']);
		return $this->fetch('update',[
			'data'		=>	$data,
			'prov'		=>	$prov,
			'group'		=>	$group,
			'shop'		=>	$shop,
			'city'		=>	$city,
			'area'		=>	$area,
		]);
	}

	public function group()
	{
		$group = DB::name('authgroup')->where('is_del = 0')->order('add_time asc')->paginate(15);
		return $this->fetch('group',[
			'group'		=>  $group,
		]);
	}

	public function delgroup()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'group_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('authgroup')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}

	public function deluser()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'user_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('user')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
	public function createGroup()
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			$data['add_time'] = time();
			$data['update_time'] = time();


			$res = DB::name('authgroup')->insert($data);

			if($res){
				$this->success('新建成功',url('index/index/index',array('con'=>'user','act'=>'index')));
			}else{
				$this->error('创建失败');
			}
		}
		return $this->fetch('create_group');
	}


	public function updateGroup($id)
	{
		if(Request::instance()->isPost()){
			$id   = Request::instance()->post('id');
			$data['title'] =  Request::instance()->post('title');
			$data['description'] = Request::instance()->post('description');
			$data['status']		= Request::instance()->post('status');
			$data['update_time'] = time();
			$res = DB::name('authgroup')->where('id= '.$id)->setField($data);		
			if($res){
				$this->success('修改成功',url('index/index/index',array('con'=>'user','act'=>'group')));
			}else{
				$this->error('修改失败');	
			}
		}
		$data = DB::name('authgroup')->where('id ='.$id)->find();
		return $this->fetch('create_group',[
			'data'	=> $data,
		]);
	}	

	/*修改 用户组的权限*/
	public function userRule($id)
	{

		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post('quan/a');
			$data = serialize($data);	
			$res = DB::name('authgroup')->where('id = '.$id)->setField('rule',$data);
			if($res){
				$this->success('权限修改成功',url('index/index/index',array('con'=>'user','act'=>'group')));
			}
		}
		$menu = $this->menu();
		foreach ($menu as $k => $v) {
			$menu[$k]['rule'] = DB::name('rule')->where("model = '".$v['menu_name']."'")->select();
		}
		$data = DB::name('authgroup')->field('id,title,rule')->where('id = '.$id)->find();
		$rule = unserialize($data['rule']);
		return $this->fetch('user_rule',[
			'menu'		=>	$menu,
			'data'		=>	$data,
			'allrule'		=>	$rule,
		]);
	}


	public function menu()
	{
		$menu = DB::name('sys_menu')->field('menu_name')->where('pid = 0')->order('orderid asc')->select();
		return $menu;
	}

	/*获取省信息*/
	public function getAddress()
	{
		$prov_data = db::name('cn_prov')->field('code,name')->select();
		return $prov_data;
	}


	public function getAuthGroup()
	{
		$group = DB::name('authgroup')->field('id,title')->select();
		return $group;
	}

	public function getShop()
	{
		$shop = DB::name('shop')->field('id,title')->select();
		return $shop;
	}

	public function getcity($pcode)
	{
		$city = db::name('cn_city')->field('code,name')->where('pcode',$pcode)->select();
		return $city;
	}

	public function getarea($pcode)
	{

		$area = db::name('cn_area')->field('code,name')->where('pcode',$pcode)->select();
		return $area;
	}

}
?>